﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Streambolics.Hp41s
{
    public class ReferenceInstructionFactory : InstructionFactory
    {
        private MethodInfo _FactoryMethod;

        public ReferenceInstructionFactory (string aName, MethodInfo aFactoryMethod)
            : base (aName)
        {
            _FactoryMethod = aFactoryMethod;
        }

        public override Instruction Parse (Tokenizer aTokenizer)
        {
            aTokenizer.Next ();
            bool indirect = aTokenizer.Current.ToUpper () == "IND";
            if (indirect)
            {
                aTokenizer.Next ();
            }
            return (Instruction)(_FactoryMethod.Invoke (null, new object[] { Reference.Parse (aTokenizer.Current), indirect }));
        }
    }
}
